var dataTD;
var rowNum = 1;
var flag = "";
$(document).ready(function(){
    dataTD=$("#mainDataTable").DataTable({
        ajax : {
            url : "t_admin/getTable",
            type : "POST",
            data :function(s){
                s.search = $('#mainDataTable').DataTable().search(this.value);
                s.flag=flag;
            }
        },
        columns: [{
            data : "id",
            render : function(data, type, row, meta) {
                var idname = "checkbox" + (parseInt(meta.row) + 1);
                return "<input type='checkbox' value='" + data + "' id='" + idname + "'>";
            }
        },{
            data : "id",
        },{
            data : "username",
        },{
            data : "isDelete",
            render: function (data, type, row) {
                var partData = data;
                if (data == "1")
                	return "不允许";
                else
                	return "允许";
            }
        },{
            data : "id",
            render : function(data, type, row) {
            	if(row.isDelete == 0)
            		return "<button class='btn btn-default btn-xs'onclick='$.deleModal("+ data+ ")'><span class='glyphicon glyphicon-minus' aria-hidden='true'></span>禁止</button>";
            	else
            		return "<button class='btn btn-default btn-xs'onclick='$.recModal("+ data+ ")'><span class='glyphicon glyphicon-minus' aria-hidden='true'></span>恢复</button>";
            },
        } ],
        dom : "<'row'<'col-md-3'B>>t<'row'<'col-md-3'l><'col-md-3'><'#mainToolbar.col-md-6'>r>t<'row'<'col-md-3'i><'col-md-9'p>>",
        initComplete: function () {
            // 更改工具栏按钮的样式
            $("#mainToolbar").empty();
            var strToolBar = "<span> 是否允许：</span><select id='flag' name='flag' class='form-control'><option value=''>全部</option>" +
                "<option value='0'>允许</option><option value='1'>不允许</option></select>&nbsp;" +
                " <button class='btn btn-default btn-sm' onclick='$(this).searchDT()'>" +
                "<span class='glyphicon glyphicon-search' aria-hidden='true'></span> 搜索</button></div>";
            $("#mainToolbar").append(strToolBar);
        },
        buttons :[{
            text : "<span class='glyphicon glyphicon-refresh' aria-hidden='true'></span> 刷新",
            className : 'btn-refresh',
            action : function(e, dt, node, config) {
                dt.ajax.reload();
                $("#headCheckbox").attr("checked", false);
            }
        }, {
            text: "<span class='glyphicon glyphicon-minus' aria-hidden='true'></span> 批量禁止登录",
            className: 'btn-add',
            action: function (e, dt, node, config) {
                $.deleModal(0);
            }
        }],
        processing : true,//是否显示加载中提示
        serverSide : true,//是否从服务器获取数据
        ordering : false,
        autoFill : true,
        displayLength : 20,//默认每页显示的页数
        lengthMenu : [ [ 1, 5, 15, 20, 50, 100, 150, -1 ], [ 1, 5, 15, 20, 50, 100, 150, "All" ] ],
        language : {
            lengthMenu : "每页显示 _MENU_ 条记录",
            zeroRecords : "没有符合条件的记录",
            infoEmpty : "暂无数据",
            processing : "加载中...",
            info : "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",
            infoFiltered : "(从 _MAX_ 条记录中过滤)",
            paginate : {
                first : "首页",
                previous : "前一页",
                next : "后一页",
                last : "尾页"
            },
            buttons: {
                selectItem:"选中%d行"
            },
            select:{
                rows:{
                    _:"选中%d行",
                    1:"选中1行"
                }
            }
        },
        drawCallback : function() {//每次表格调用完成时调用
            rowNum = 1;
            $('[data-toggle="popover"]').popover({
                trigger: 'hover ',
            });
        },
        rowCallback : function(row, data, displayIndex) {
            $(row).addClass('domrow');
            $('td:eq(1)', row).html($("#mainDataTable").dataTable().fnSettings()._iDisplayStart + rowNum++);
            return row;
        }
    });
});


$("#headCheckbox").change(function() {
    $("input[id^='checkbox']").prop("checked", $("#headCheckbox").is(':checked'));
});

$.fn.searchDT = function () {
    flag = $("#flag").val();
    dataTD.search("").draw();
};

$.deleModal = function(ids){
    var count = 1;
    if (ids == 0) {
        var count = $("input:checkbox[id^='checkbox']:checked").length;
        if (count == 0) {
            $.alert("请选择不允许登录的账号 ", 2);
            return;
        } else {
        	ids = "";
            $("input:checkbox[id^='checkbox']:checked").each(function () {
            	ids += $(this).val() + ",";
            });
            ids = ids.substring(0, ids.length - 1);
        }
    }
    swal({
            title:  '确定不让这' + count + '个账号登录吗?',
            type: "warning",
            showCancelButton: true,
            cancelButtonText: "取消",
            confirmButtonClass: "btn-danger",
            confirmButtonText: "确定",
            closeOnConfirm: false
        },
        function(){
            $.deletes(ids);
        });
}

$.deletes=function(ids){
    $.ajax({
        url : "t_admin/delete",
        type : "POST",
        data : {
        	ids : ids
        },
        dataType : 'json',
        success : function(msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    $.alert("成功！", 1);
                    dataTD.ajax.reload(null, false);
                } else {
                    $.alert("失败！", 4);
                }
            } else {
                $.alert('系统错误！', 4);
            }
        },
        error : function(msg) {
            $.alert("系统错误！", 4);
        }
    });
}

$.recModal = function(ids){
    var count = 1;
    if (ids == 0) {
        var count = $("input:checkbox[id^='checkbox']:checked").length;
        if (count == 0) {
            $.alert("请选择允许登录的账号 ", 2);
            return;
        } else {
        	ids = "";
            $("input:checkbox[id^='checkbox']:checked").each(function () {
            	ids += $(this).val() + ",";
            });
            ids = ids.substring(0, ids.length - 1);
        }
    }
    swal({
            title:  '确定让这' + count + '个账号登录吗?',
            type: "warning",
            showCancelButton: true,
            cancelButtonText: "取消",
            confirmButtonClass: "btn-danger",
            confirmButtonText: "确定",
            closeOnConfirm: false
        },
        function(){
            $.recover(ids);
        });
}

$.recover=function(ids){
    $.ajax({
        url : "t_admin/recover",
        type : "POST",
        data : {
        	ids : ids
        },
        dataType : 'json',
        success : function(msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    $.alert("成功！", 1);
                    dataTD.ajax.reload(null, false);
                } else {
                    $.alert("失败！", 4);
                }
            } else {
                $.alert('系统错误！', 4);
            }
        },
        error : function(msg) {
            $.alert("系统错误！", 4);
        }
    });
}

